﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Util;

namespace DAO
{
    /// <summary>
    /// Summary description for UserModule
    /// </summary>
    public class UserModule
    {
        DBConnection DBConnection;
        public UserModule()
        {
            DBConnection = new DBConnection();
            //
            // TODO: Add constructor logic here
            //
        }
        /// <summary>
        /// Get all DTO.UserModule instance from DB.
        /// </summary>
        /// <returns>List<> of all DTO.UserModule instances.</returns>
        public List<DTO.UserModule> GetAllUserModules()
        {
            Log.LogInfo("Begin GetAllUserModules()");
            String query = "SELECT * FROM [UserModule];";
            SqlDataReader reader = DBConnection.ExecuteReader(query);
            List<DTO.UserModule> userModuleDTOs = new List<DTO.UserModule>();
            try
            {
                while (reader.Read())
                {
                    userModuleDTOs.Add(new DTO.UserModule(reader));
                }
            }
            catch (Exception ex)
            {
                Log.LogError("Error GetAllUserModules(): " + ex.StackTrace);
            }
            DBConnection.Close();
            Log.LogInfo("End GetAllUserModules()");
            return userModuleDTOs;
        }

        /// <summary>
        /// Insert UserModule into database.
        /// </summary>
        /// <param name="UserModule">DTO.UserModule instance to insert into database.</param>
        public int InsertUserModule(DTO.UserModule UserModule)
        {
            Log.LogInfo("Begin InsertUserModlue()");
            String query = "";
            query += "INSERT INTO USER_MODULE (UserID,ModuleID) VALUES ('" + UserModule.UserID + "','" + UserModule.ModuleID + "')";
            query += "SET IDENTITY_INSERT USER_MODULE OFF";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Insert " + result.ToString() + "row(s).");
            Log.LogInfo("End InsertUserModlue()");
            return result;
        }

        /// <summary>
        /// Update UserModule info into database.
        /// </summary>
        /// <param name="UserModule">UserModule instance to be updated.</param>
        public int UpdateUserModule(DTO.UserModule UserModule)
        {
            return 1;

        }
        /// <summary>
        /// Delete a specific UserModule from DB.
        /// </summary>
        /// <param name="UserModule">The UserModule to be deleted.</param>
        public int DeleteUserModule(DTO.UserModule UserModule)
        {
            Log.LogInfo("Begin DeleteUserModule()");
            String query = "DELETE FROM [UserModule] WHERE [UserModule].[UserID]='" + UserModule.UserID + "' AND [UserModule].[ModuleId]='" + UserModule.ModuleID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteUserModule()");
            return result;
        }

        /// <summary>
        /// Delete a UserModule in database using ID.
        /// </summary>
        /// <param name="ID">UserModule ID.</param>
        public int DeleteUserModule(String UserId, string UserModuleID)
        {
            Log.LogInfo("Begin DeleteUserModule()");
            String query = "DELETE FROM [UserModule] [UserModule].[UserID]='" + UserId + "' AND [UserModule].[ModuleId]='" + UserModuleID + "';";
            int result = DBConnection.ExecuteNonQuery(query);
            DBConnection.Close();
            Log.LogInfo("Deleted " + result.ToString() + "row(s).");
            Log.LogInfo("End DeleteUserModule()");
            return result;
        }
    }
}